home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0386.arc
/
AIEYE1.LTG
< prev
next >
Wrap
Text File
|
1986-03-03
|
2KB
|
51 lines
Listing 1
Match in Prolog
% Match(P,E) is a predicate that instantiates its arguments
% such that P is a pattern template matching the pattern
% example E. A pattern template is a list or nested list
% containing atomics and pattern operators. A pattern example
% is a list or nested list containing atomics only (can you
% see why we exclude pattern operators in the pattern example?).
%
% Pattern operators in this version include ? (match one element)
% and + (match one or more elements).
[1] match([],[]). % Two nil lists match
[2] match([EH | PT], [EH | ET]) :- % Lists starting with same thing
[3] match(PT,ET). % match if their tails match
[4] match([+ | PT], E) :- % A + matches 1 or more elements,
[5] append([EH | _],ET,E), % so instantiate non-nil tail of E
[6] match(PT,ET). % & succeed if it matches tail of P
[7] match([? | PT], [EH | ET]) :- % ? matches one element, so succeed
[8] match(PT, ET). % if tails of P & E match
[9] match([PH | PT], [EH | ET]) :- % Nested lists match if
[10] match(PH,EH), % their heads match
[11] match(PT,ET). % and their tails match
% Induce instantiates its arguments such that P is a pattern
% template matching both example lists E1 and E2.
[12] induce(E1,E2,P) :- match(P,E1), match(P,E2).
% Classify instantiates its arguments such that P is a member of
% the list of pattern templates PL and P matches the example
% list E.
[13] classify(E,PL,P) :- member(P,PL), match(P,E).
% Append(L1,L2,L3) instantiates its arguments such that
% the list L3 is the result of concatenating lists L1 and L2.è
[14] append([],L,L). % Appending a list to nil is no-op
[15] append([X|L1],L2,[X|L3]) :- % Appending 2nd arg to 1st means
append(L1,L2,L3). % append 2nd arg to 1st's TAIL then
% add head of 1st, X, to result, L3